package utils 
{
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFormatAlign;
	import flash.text.TextFieldAutoSize;
	import flash.geom.*
    import flash.display.*
	/**
	 * ...
	 * @author corasanu
	 */
	public class SimpleButton extends Sprite
	{
	    protected var btnWidth:int;
	    protected var btnHeight:int;
	    
	    protected var _label:String;
	    protected var _action:Function;
	    protected var _params:Array;
		protected var labelTextField:TextField;
		
		private var fontSize:int = 11;
		
		private var mouseDown:Boolean;
		
		public function SimpleButton(label:String, action:Function, params:Array = null) 
		{
			_label = label;
			_action = action;
			_params = params;
		}
		
		protected function init():void
		{	
		    setListeners("addEventListener");	
			buttonMode = true;	
			
			labelTextField = new TextField();
			addChild(labelTextField);
			labelTextField.width = btnWidth;
			labelTextField.multiline = false;
			labelTextField.mouseEnabled = false;
			labelTextField.selectable = false;
			labelTextField.autoSize = TextFieldAutoSize.CENTER;
			
			var tf:TextFormat = new TextFormat("Verdana", fontSize, 0xAAAAAA);
			tf.align = TextFormatAlign.CENTER;
			labelTextField.defaultTextFormat = tf;	
			
			labelTextField.text = _label;	
			updateLabel();	
			
			drawBtn([0x111111, 0x000000], 0x555555);		
		}
		
		protected function drawBtn(fillColors:Array, lineColor:uint):void
		{
		    graphics.clear();
		    graphics.lineStyle(1, lineColor, 0.8, true)
			var fillType:String = GradientType.LINEAR;
            var alphas:Array = [1, 1];
            var ratios:Array = [0x00, 0xFF];
            var matr:Matrix = new Matrix();            
            matr.createGradientBox(btnWidth, btnHeight, Math.PI/180 * 90, 0, 0);
            var spreadMethod:String = SpreadMethod.PAD;
            this.graphics.beginGradientFill(fillType, fillColors, alphas, ratios, matr, spreadMethod);  
			graphics.drawRoundRect(0, 0, btnWidth, btnHeight, 6, 6);
			graphics.endFill()
			
			updateLabel();
		}	
		
		protected function updateLabel():void
		{
		    labelTextField.y = (height - labelTextField.height) / 2;		
		}	
		
		protected function setListeners(action:String):void
		{
		    this[action](MouseEvent.CLICK, callFunction);
		    this[action](MouseEvent.ROLL_OVER, onRollOver);
		    this[action](MouseEvent.ROLL_OUT, onRollOut);
		    this[action](MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		protected function onRollOver(event:MouseEvent):void
		{	
		    if(mouseDown) return;	
		        
		    drawBtn([0x333333, 0x222222], 0xCCCCCC);
		    
		    var tf:TextFormat = new TextFormat("Verdana", fontSize, 0xEEEEEE);
			tf.align = TextFormatAlign.CENTER;
			labelTextField.setTextFormat(tf);
		}
		
		protected function onRollOut(event:MouseEvent):void
		{
		    if(mouseDown) return;
		    
		    drawBtn([0x111111, 0x000000], 0x555555);
		    
		    var tf:TextFormat = new TextFormat("Verdana", fontSize, 0xAAAAAA);
			tf.align = TextFormatAlign.CENTER;
			labelTextField.setTextFormat(tf);
		}	
		
		protected function onMouseDown(event:MouseEvent):void
		{		    
		    mouseDown = true;
		    
		    drawBtn([0x222222, 0x333333], 0xCCCCCC);
		    
		    var tf:TextFormat = new TextFormat("Verdana", fontSize, 0xAAAAAA);
			tf.align = TextFormatAlign.CENTER;
			labelTextField.setTextFormat(tf);
			
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		    addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}	
		
		protected function onMouseUp(event:MouseEvent):void
		{		    
		    mouseDown = false;
		    
		    drawBtn([0x111111, 0x000000], 0x555555);
		    
		    var tf:TextFormat = new TextFormat("Verdana", fontSize, 0xAAAAAA);
			tf.align = TextFormatAlign.CENTER;
			labelTextField.setTextFormat(tf);
			
		    stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		    removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
		}		
		
		protected function callFunction(event:MouseEvent):void
		{
		    mouseDown = false;
		    
		    if(_params)
		        _action.apply(null, _params);
		    else
		        _action.apply();
		}	
		
		public function get label():String
		{
		    return _label;
		}	
		
		public function set label(value:String):void
		{
		    _label = value;
		    labelTextField.text = value;
		    updateLabel();
		}
		
		public function setSize(w:int, h:int):void
		{
		    btnWidth = w;
		    btnHeight = h;
		    
		    drawBtn([0x111111, 0x000000], 0x555555);
		}
	}
}
